#include "SDLButton.h"

SDLButton::SDLButton() {
	m_Handle = 0;
	m_btnText = "Default Text";
	m_btnTextColor.r = 0x00;
	m_btnTextColor.g = 0x00;
	m_btnTextColor.b = 0x00;
	m_btnFrameColor.r = 0xff;
	m_btnFrameColor.g = 0xff;
	m_btnFrameColor.b = 0x00;
	m_btnBGColor.r = 0xff;
	m_btnBGColor.g = 0x00;
	m_btnBGColor.b = 0xff;
	m_btnLocation.h = 0;
	m_btnLocation.w = 0;
	m_btnLocation.x = 0;
	m_btnLocation.y = 0;
	m_btnNormal = 0;
	m_btnActive = 0;
	m_btnParent = 0;
	m_btnEvent = 0;
}

SDLButton::SDLButton(ObjHandle handle, std::string btnText, SDL_Color btnTextColor, SDL_Color btnFrameColor,
					SDL_Color btnBGColor, SDL_Rect btnLocation, SDL_Event *btnEvent, SDL_Surface *btnParent, 
					TTF_Font *btnFont) {
	m_Handle = handle;
	m_btnText = btnText;
	m_btnTextColor = btnTextColor;
	m_btnFrameColor = btnFrameColor;
	m_btnBGColor = btnBGColor;
	m_btnLocation = btnLocation;
	m_btnNormal = 0;
	m_btnActive = 0;
	m_btnParent = btnParent;
	m_btnEvent = btnEvent;
	m_btnFont = btnFont;
	m_btnIsActive = false;
	//do graphics code stuff
	render();
}

SDLButton::SDLButton(ObjHandle handle, SDL_Rect btnLocation, SDL_Rect btnActiveLoc, SDL_Rect btnPressedLoc, 
					SDL_Event *btnEvent, SDL_Surface *btnParent, SDLObject *pParent, const char *btnGraphicsFilename) {
	m_Handle = handle;
	m_btnLocation = btnLocation;
	SDL_Surface *load = load_image(btnGraphicsFilename);
	SDL_Surface *tempNormal = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, btnActiveLoc.w, btnActiveLoc.h, 32, 0, 0, 0, 0);
	SDL_Surface *tempActive = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, btnPressedLoc.w, btnPressedLoc.h, 32, 0, 0, 0, 0);
	apply_surface_from(0, 0, load, &btnActiveLoc, tempNormal); 
	apply_surface_from(0, 0, load, &btnPressedLoc, tempActive);
	m_btnNormal = SDL_DisplayFormat(tempNormal);
	m_btnActive = SDL_DisplayFormat(tempActive);
	SDL_SetColorKey(m_btnNormal, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(m_btnNormal->format, 41, 239, 214));
	SDL_SetColorKey(m_btnActive, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(m_btnActive->format, 41, 239, 214));
	m_btnParent = btnParent;
	m_btnEvent = btnEvent;
	m_btnFont = 0;
	m_btnIsActive = false;
	m_pParent = pParent;
}

void SDLButton::render() {
	SDL_Rect dst;
	SDL_Surface *buffer;
	int h = 0, w = 0;
	m_btnNormal = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, m_btnLocation.w, m_btnLocation.h, 32, 0, 0, 0, 0);
	m_btnActive = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, m_btnLocation.w, m_btnLocation.h, 32, 0, 0, 0, 0);
	dst.x = 0;
	dst.y = 0;
	dst.h = m_btnLocation.h;
	dst.w = m_btnLocation.w;
	SDL_FillRect(m_btnNormal, &dst, COMBINE(m_btnFrameColor.r, m_btnFrameColor.g, m_btnFrameColor.b));
	SDL_FillRect(m_btnActive, &dst, COMBINE(m_btnFrameColor.r, m_btnFrameColor.g, m_btnFrameColor.b));
	dst.x = 1;
	dst.y = 1;
	dst.h -= 2;
	dst.w -= 2;
	SDL_FillRect(m_btnNormal, &dst, COMBINE(m_btnBGColor.r, m_btnBGColor.g, m_btnBGColor.b));
	dst.x += 2;
	dst.y += 2;
	//dst.h--;
	//dst.w--;
	SDL_FillRect(m_btnActive, &dst, COMBINE(m_btnBGColor.r, m_btnBGColor.g, m_btnBGColor.b));
	buffer = TTF_RenderText_Solid(m_btnFont, m_btnText.c_str(), m_btnTextColor);
	dst.x = (m_btnLocation.w - buffer->w) / 2;
	dst.y = (m_btnLocation.h - buffer->h) / 2;
	//SDL_FreeSurface(m_btnNormal);
	apply_surface(dst.x, dst.y, buffer, m_btnNormal);
	apply_surface(dst.x + 2, dst.y + 2, buffer, m_btnActive);
	SDL_FreeSurface(buffer);
}

void SDLButton::display() {
	//render();
	if(!m_btnIsActive) apply_surface(m_btnLocation.x, m_btnLocation.y, m_btnNormal, m_btnParent);
	else apply_surface(m_btnLocation.x, m_btnLocation.y, m_btnActive, m_btnParent);
}

void SDLButton::inputHandler() {
	int x = 0, y = 0;
	switch(m_btnEvent->type) {
	case SDL_MOUSEBUTTONDOWN:
		x = m_btnEvent->button.x;
		y = m_btnEvent->button.y;
		if((x > m_btnLocation.x) && (x < m_btnLocation.x + m_btnLocation.w) && (y > m_btnLocation.y) && (y < m_btnLocation.y + m_btnLocation.h)) {
			if(!m_btnIsActive) { 
				m_pParent->doAction(m_Handle, SDLBUTTONDOWN);
				m_btnIsActive = true;
			}
		}
		break;
	case SDL_MOUSEBUTTONUP:
		x = m_btnEvent->button.x;
		y = m_btnEvent->button.y;
		if((x > m_btnLocation.x) && (x < m_btnLocation.x + m_btnLocation.w) && (y > m_btnLocation.y) && (y < m_btnLocation.y + m_btnLocation.h)) {
			if(m_btnIsActive) {
				m_pParent->doAction(m_Handle, SDLBUTTONUP);
				m_btnIsActive = false;
			}
		}
		break;
	case SDL_MOUSEMOTION:
		x = m_btnEvent->motion.x;
		y = m_btnEvent->motion.y;
		if(!((x > m_btnLocation.x) && (x < m_btnLocation.x + m_btnLocation.w) && (y > m_btnLocation.y) && (y < m_btnLocation.y + m_btnLocation.h))) {
			m_btnIsActive = false;
		}
	}
}
